package com.starinside.wp.controller.api;

import com.starinside.wp.domain.Children;
import com.starinside.wp.domain.WxUser;
import com.starinside.wp.domain.cookbook.SimpleCookbook;
import com.starinside.wp.foundation.dto.StaffDTO;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.query.base.PagerQuery;
import com.starinside.wp.service.ChildrenService;
import com.starinside.wp.service.cookbook.SimpleCookbookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;


@RestController
@RequestMapping("/pad/cookbook")
@Slf4j
@Api(tags = "简易食谱，查询")
public class APISimpleCookbookController {

	@Autowired
	SimpleCookbookService baseService;

	@Autowired
	ChildrenService childrenService;

	@ApiOperation(value = "获取简易食谱列表")
	@GetMapping("/simples")
	public StatusResponse list(@RequestHeader(TICKET) String ticket, HttpServletRequest req, HttpServletResponse resp,
							   String childrenId, PagerQuery pagerQuery){

		if(StringUtils.isEmpty(childrenId)) {
			WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");

			if (loginUser.getDefaultBabyId() == null) {
				return StatusResponse.error(ErrorCode.CHILDREN_NOT_EXIST, "当前家长没有就读的宝宝");
			}

			childrenId = loginUser.getDefaultBabyId();
		}

		Children c = childrenService.getById(childrenId);

		Page<SimpleCookbook> page = baseService.page(c.getAreaId(), pagerQuery);

		return StatusResponse.success(page);
	}


	@ApiOperation(value = "获取一个简易食谱对象")
	@GetMapping("/simple/{id}")
	public StatusResponse get(@RequestHeader(TICKET) String ticket, HttpServletRequest req, HttpServletResponse resp,
							  String childrenId, @PathVariable String id){

		if(StringUtils.isEmpty(childrenId)) {
			WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");

			if (loginUser.getDefaultBabyId() == null) {
				return StatusResponse.error(ErrorCode.CHILDREN_NOT_EXIST, "当前家长没有就读的宝宝");
			}

			childrenId = loginUser.getDefaultBabyId();
		}

		Children c = childrenService.getById(childrenId);

		SimpleCookbook ret = baseService.get(c.getAreaId(), id);

		return StatusResponse.success(ret);
	}

}
